using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Weight.Base;

namespace  RFCard
{
    public class IdentifyJCX : ICard
    {
        /// <summary>
        /// 判断是否已经打开
        /// </summary>
        private bool mIsOpen = false;
        private int mTimeOutTime = 500;
        /// <summary>
        /// 比对卡号集合
        /// </summary>
        private List<string> mComparisons = null;
       /// <summary>
       /// 头像路径
       /// </summary>
        byte[] headphotoPathTmp = new byte[256];
        /// <summary>
        /// 前卡片图像路径
        /// </summary>
        byte[] frontphotoPathTmp = new byte[256];

        /// <summary>
        /// 后卡片路径
        /// </summary>
        byte[] backphotoPathTmp = new byte[256];

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public ResultEntity Init()
        {
            return ResultEntity.Success("身份证识别金诚信，该设备不支持该功能");
        }

        /// <summary>
        /// 打开设备
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public ResultEntity Open(OpenEntity entity)
        {
            var result = ResultEntity.Default();
            try
            {
                if(mIsOpen)
                {
                    return ResultEntity.Error("身份证读卡设备(金诚信) 已经打开，不能重复打开!");
                }
                byte[] Extport = new byte[2];
                if(string.IsNullOrEmpty(entity.ExtBox))
                {
                    return ResultEntity.Error("身份证读卡设备(金诚信)参数错误，原因:扩展盒设置不能为空!");
                }
                Extport = System.Text.Encoding.Default.GetBytes(entity.ExtBox);
                int   ret = appdll.GHC_Dev_Connect(entity.Port, Extport[0], entity.BaudRate);
                if(0 == ret)
                {
                    result = ResultEntity.Success("身份证读卡设备(金诚信)打开成功!");
                    mIsOpen = true;
                }
                else
                {
                    result = ResultEntity.Success("身份证读卡设备(金诚信)打开失败!");
                }
            }
            catch(Exception ex)
            {
                result = ResultEntity.Error("身份证读卡设备(金诚信)打开异常,异常信息:"+ex.Message+","+ex.StackTrace);
            }

            return result;
        }

        /// <summary>
        /// 获取卡号
        /// </summary>
        /// <returns></returns>
        public ResultEntity<ResultValEntity> GetCardNo()
        {
            ResultValEntity entity = new ResultValEntity();
           var result = ResultEntity<ResultValEntity>.Default(entity,"");
           try
            {
                byte[] wzInfo = new byte[256];
                int  ret = appdll.GHC_IDCard_ReadCard(mTimeOutTime);
                if(0 == ret)
                {
                    appdll.GHC_IDCard_GetCardInfo(1, wzInfo);
                    string name = System.Text.Encoding.Default.GetString(wzInfo);
                    if(name.IndexOf('\0') > 0)
                    {
                        entity.Person.Name = name.Substring(0, name.IndexOf('\0'));
                    }
                    else
                    {
                        entity.Person.Name = name;
                    }
                   

                    appdll.GHC_IDCard_GetCardInfo(5, wzInfo);
                    string addr = System.Text.Encoding.Default.GetString(wzInfo);
                    if(addr.IndexOf('\0') > 0)
                    {
                        entity.Person.Address = addr.Substring(0,addr.IndexOf('\0'));
                    }
                    else
                    {
                        entity.Person.Address = addr;
                    }
                    

                    appdll.GHC_IDCard_GetCardInfo(6, wzInfo);
                    string id= System.Text.Encoding.Default.GetString(wzInfo);
                    if (id.IndexOf('\0') > 0)
                    {
                        id = id.Substring(0, id.IndexOf('\0'));
                    }
                   
                    entity.Person.PersonId = getLimitNo(id);
                    entity.val = entity.Person.PersonId;
                    result = ResultEntity<ResultValEntity>.Success(entity, entity.val);

                    string dir = Environment.CurrentDirectory + "\\CardImg\\";
                    if(!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    string headPath = dir+"head" + id + ".jpg";
                    string frontPath = dir+"cardFront" + id + ".jpg";
                    string backpath = dir + "cardBack" + id + ".jpg";
                    headphotoPathTmp = System.Text.Encoding.Default.GetBytes(headPath);
                    frontphotoPathTmp = System.Text.Encoding.Default.GetBytes(frontPath);
                    backphotoPathTmp = System.Text.Encoding.Default.GetBytes(backpath);
                    int PhotoType = 0;
                    int resHead = appdll.GHC_IDCard_CreateHeadPhoto(PhotoType, headphotoPathTmp);
                    if (resHead == 0) entity.Person.ImgHead = headPath;

                    int resFront= appdll.GHC_IDCard_CreateFrontPhoto(PhotoType, frontphotoPathTmp);
                    if (resFront == 0) entity.Person.ImgCardFront = frontPath;

                    int resBack = appdll.GHC_IDCard_CreateBackPhoto(PhotoType, backphotoPathTmp);
                    if (resBack == 0) entity.Person.ImgCardBack = backpath;
                }
                else
                {
                    result = ResultEntity<ResultValEntity>.Error(entity, "身份证读卡设备(金诚信)，读卡失败,请移除卡片重新放入");
                }
            }
            catch(Exception ex)
            {
                result = ResultEntity<ResultValEntity>.Error(entity,"身份证读卡设备(金诚信) 读卡异常，异常信息:"+ex.Message+","+ex.StackTrace);
            }

            return result;
        }

        /// <summary>
        /// 读卡
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public ResultEntity<ResultValEntity> Read(ReadParamEntity entity)
        {
            ResultValEntity valEntity = new ResultValEntity();
            return ResultEntity<ResultValEntity>.Success(valEntity,"身份证读卡设备(金诚信)，该设备不支持读卡功能!");
        }

        /// <summary>
        /// 写卡
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public ResultEntity Write(WriteParamEntity entity)
        {
            return ResultEntity.Success("身份证读卡设备(金诚信)，该设备不支持写卡功能!");
        }

        public ResultEntity Clear(ClearParamEntity entity)
        {
            return ResultEntity.Success("身份证读卡设备(金诚信)，该设备不支持清卡功能!");
        }

        /// <summary>
        /// 获取设备信息
        /// </summary>
        /// <returns></returns>
        public ResultEntity GetDeviceInfo()
        {
            StringBuilder contentBuider = new StringBuilder("厂商:金诚信\r\n");
            contentBuider.Append("依赖库:GHC_GetIDCardInfo.dll、HID_sdtapi.dll、sdtapi.dll、WltRS.dll");
            return ResultEntity.Success(contentBuider.ToString());
        }

        /// <summary>
        /// 设置比对信息
        /// </summary>
        /// <param name="cardNos"></param>
        public void SetComparison(List<string> cardNos)
        {
            mComparisons = cardNos;
        }

        /// <summary>
        /// 筛选卡号
        /// </summary>
        /// <returns></returns>
        public string getLimitNo(string id)
        {
            if(mComparisons !=null && mComparisons.Count >0 && id.Length > 0)
            {
                if(mComparisons.Contains(id))
                {
                    return id;
                }
                else
                {
                    return "未匹配的卡号";
                }
            }
            else
            {
                return id;
            }
        }

        /// <summary>
        /// 关闭设备
        /// </summary>
        /// <returns></returns>
        public ResultEntity Close()
        {
            var result = ResultEntity.Default();
            try
            {
               int res =  appdll.GHC_Dev_Disconnect();
                if(res == 0)
                {
                    result = ResultEntity.Success("身份证读卡设备(金诚信) 关闭成功!");
                    mIsOpen = false;
                }
                else
                {
                    result = ResultEntity.Error("身份证读卡设备(金诚信) 关闭失败!");
                }
            }
            catch(Exception ex)
            {
                result = ResultEntity.Error("身份证读卡设备(金诚信) 关闭异常，异常信息:"+ex.Message+","+ex.StackTrace);
            }

            return result;
        }
    }
}


